import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import "../styles" as Styles

/**
 * ConfirmDialog - 统一风格的确认弹窗
 * 基于 CustomDialog 实现，无边框、带模态遮罩，视觉与 CreateTaskDialog 保持一致。
 */
CustomDialog {
    id: confirmDialog

    // 对外属性（兼容旧接口）
    property string messageText: ""
    property string detailText: ""
    property string message: ""
    property string confirmText: qsTr("确定")
    property string cancelText: qsTr("取消")
    property string iconText: "⚠️"
    property var callback: null
    property var cancelCallback: null
    property bool confirmEnabled: true

    // 内部状态
    property bool actionTriggered: false
    readonly property string _resolvedMessage: message !== "" ? message : messageText
    readonly property bool _hasDetail: detailText && detailText.trim() !== ""

    dialogWidth: 460
    dialogHeight: _hasDetail ? 280 : 240
    showOkButton: true
    showCancelButton: cancelText !== ""
    okButtonText: confirmText
    cancelButtonText: cancelText
    okButtonEnabled: confirmEnabled && !actionTriggered

    onOpened: {
        if (!title || title.trim().length === 0) {
            title = qsTr("确认操作")
        }
        actionTriggered = false
    }

    onAccepted: {
        if (actionTriggered) {
            return
        }
        actionTriggered = true
        close()
        if (callback) {
            callback()
        }
    }

    onRejected: {
        close()
        if (cancelCallback) {
            cancelCallback()
        }
    }

    contentItem: ColumnLayout {
        width: parent.width
        spacing: Styles.Theme.spacingLarge

        RowLayout {
            Layout.fillWidth: true
            spacing: Styles.Theme.spacingNormal

            Rectangle {
                Layout.preferredWidth: 56
                Layout.preferredHeight: 56
                radius: 28
                color: Styles.Theme.backgroundSecondary
                border.color: Styles.Theme.border
                border.width: 1

                Text {
                    anchors.centerIn: parent
                    text: iconText
                    font.pixelSize: 24
                }
            }

            ColumnLayout {
                Layout.fillWidth: true
                spacing: Styles.Theme.spacingSmall

                Text {
                    text: _resolvedMessage || qsTr("确定要执行该操作吗？")
                    color: Styles.Theme.textPrimary
                    font.pixelSize: Styles.Theme.fontSizeMedium
                    font.weight: Styles.Theme.fontWeightMedium
                    wrapMode: Text.WordWrap
                    Layout.fillWidth: true
                }

                Text {
                    visible: _hasDetail
                    text: detailText
                    color: Styles.Theme.textSecondary
                    font.pixelSize: Styles.Theme.fontSizeSmall
                    wrapMode: Text.WordWrap
                    Layout.fillWidth: true
                }
            }
        }

        Rectangle {
            Layout.fillWidth: true
            Layout.preferredHeight: 1
            color: Styles.Theme.divider
            opacity: 0.4
        }

        Text {
            Layout.fillWidth: true
            visible: !confirmEnabled
            text: qsTr("请完成必要条件后再继续。")
            color: Styles.Theme.textTertiary
            font.pixelSize: Styles.Theme.fontSizeSmall
        }
    }
}
